import java.util.Scanner;

public class 买卖股票 {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int n=0;
        n=scan.nextInt();
        int []price=new int[n];
        int res=0;
        for (int i = 0; i < n; i++) {
            price[i]=scan.nextInt();
        }
        for (int i = 0; i < n-1; i++) {
            int temp=0;
            if((temp=price[i+1]-price[i])>0)
                res+=temp;
        }
        System.out.println(res);
    }
}
